﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Transactions;
using Components;
using Model;
using MsDAL;
using System.ComponentModel;
using ServiceHelper.Util;

namespace ServiceHelper
{
    [DataObjectAttribute]
    public partial class Tic1a00Manager : Tic1a00Base
    {
        private static Tic1a00Manager _entity;

        public static Tic1a00Manager Entity
        {
            get
            {
                if (_entity == null)
                    _entity = new Tic1a00Manager();
                return _entity;
            }
        }

        #region 个人代码

        public string SaveData(Hashtable tb, DataTable dtl, DataTable deldata, string action, string usercode)
        {
            var lst = new List<SqlApp>();

            Tic1a00 model = null;
            if ("A" == action && (dtl == null || dtl.Rows.Count == 0)) return "明细中没有数据，不需要保存！";

            string appstr = Context.GetAppByUserCode(usercode);

            using (var tran = new TransactionScope())
            {
                if ("A" == action) //新增
                {
                    //校验重复项
                    var formSubCodes = new List<string>();
                    foreach (DataRow row in dtl.Rows)
                        formSubCodes.Add(Validation.ToString(row["subcode"]));

                    var exists = FindExists(formSubCodes);
                    if (exists != null)
                        return string.Format("编码{0}已存在，请调整", string.Join("、", exists.ToArray()));

                    var subtype = Validation.ToInt(tb["subtype"]);
                    foreach (DataRow row in dtl.Rows)
                    {
                        model = new Tic1a00();
                        model.Subtype = subtype;
                        model.Subtypedesc = subtype == 1 ? "收入" : "支出";
                        model.Substatue = 1;
                        model.Substatusdesc = "正常";
                        model.Appid = ServerContext.ComCode;
                        model.Subid = Validation.ToInt(Sys0a60Manager.Entity.CreateSn("tic1a00_id"));
                        model.Subcode = Validation.ToString(row["subcode"]);
                        model.Subname = Validation.ToString(row["subname"]);
                        model.Subcreatetime = DateTime.Now;
                        model.Usercode = appstr;

                        lst.Add(this.Add(model));
                    }

                }

                var msg = CommonManager.Entity.SaveData(lst);

                if (string.IsNullOrEmpty(msg)) //success
                {
                    tran.Complete();
                }

                return msg;
            }
        }


        public string SaveEditData(Hashtable tb, string action, string usercode)
        {
            var lst = new List<SqlApp>();
            
            Tic1a00 model = null;
            string appstr = Context.GetAppByUserCode(usercode);

            using (var tran = new TransactionScope())
            {
                if ("M" == action)
                {
                    model = this.GetModelById(Components.Validation.ToInt(tb["subid"]));
                    if (null == model) return "该编辑记录可能已经被删除！";
                    Components.CommonUtil.GetInstance.CopyProperties(tb, model);

                    lst.Add(this.Update(model));
                }
                else if ("D" == action) //删除
                {
                    model = this.GetModelById(Components.Validation.ToInt(tb["subid"]));
                    if (null == model) return "该编辑记录可能已经被删除！";

                    if (model.Substatue != 1)
                    {
                        return "只有正常的项目才能被删除";
                    }

                    model.Substatue = 4; //删除
                    model.Substatusdesc = "已删除"; //删除
                    lst.Add(this.Update(model));
                }

                var msg = CommonManager.Entity.SaveData(lst);

                if (string.IsNullOrEmpty(msg)) //success
                {
                    tran.Complete();
                }

                return msg;
            }
        }

        #endregion

        private List<string> FindExists(List<string> formSubCodes)
        {
            //获取数据库的subcode
            var tbList = CommonManager.Entity.ExecDBFun("getsubcodelist", QB.Entity.AddParam("appid", ServerContext.ComCode));
            if (null != tbList)
            {
                var findCode = new List<string>();
                foreach (DataRow row in tbList.Rows)
                {
                    var code = Validation.ToString(row[0]);

                    if (formSubCodes.IndexOf(code) > -1)
                        findCode.Add(code);
                }

                if (findCode.Count > 0)
                {
                    return findCode;
                }
            }

            return null;
        }
    }
}



